window.onload = function () {
    TabBar('home');
    axios.defaults.withCredentials = true;
    //获取用户登录
    axios.get('https://elm.cangdu.org/v1/user').then(res => {
        // console.log(res);
        if(res.data.status==0){
            $('#kh_tomyup').html(`登录/注册`).click(function(){
                window.location.href=`/pages/Singin/singin.html`
            })
        }else{
            $('#kh_tomyup').html(`<img src='/img/导航栏/个人.png' class='kh_gerentouxiang'>`).click(function(){
                window.location.href=`/pages/my/my.html`
        })
    }

    })

    //轮播图按钮
    let kh_waimainav=document.getElementsByClassName('kh_waimainav')[0]
    let kh_lunbotu1=document.getElementsByClassName("kh_lunbotu1")[0]
    let kh_lunbotu2=document.getElementsByClassName("kh_lunbotu1")[0]
    let kh_zouleft=document.getElementsByClassName("kh_zouleft")[0]
    let kh_zouright=document.getElementsByClassName("kh_zouright")[0]
    kh_waimainav.addEventListener('mousemove',function(){
         
        if(kh_waimainav.scrollLeft>250){
        kh_zouleft.style.backgroundColor='silver'
        kh_zouright.style.backgroundColor='#007aff'
        kh_waimainav.scrollLeft=kh_waimainav.scrollLeft+250
    }else{
        kh_zouleft.style.backgroundColor='#007aff'
        kh_zouright.style.backgroundColor='silver'
        kh_waimainav.scrollLeft=0
    }
    })
   
    
  

    //获取餐馆
    let kh_Box = document.getElementsByClassName("kh_Box")

    $ajax({
        url: 'https://elm.cangdu.org/shopping/restaurants?latitude=31.22967&longitude=121.4762',
        method: 'get',
        async: true,
        success: function (data) {
            for (let i = 0; i < data.length; i++) {
                // console.log(data);
                $create('kh_waimaidiv', 'kh_Box',
                    `<div class='kh_CGimg'>\
                <img src='//elm.cangdu.org/img/${data[i].image_path}'></div>
                <div class='kh_boxcenter'>
                <div class='kh_boxtop'>
                <div class='kh_boxtopone'><span class='kh_pp'>品牌</span>
                <span class='kh_name' >${data[i].name}</span>
                </div>
               
                 <div class='kh_bzp'>保 准 票</div>
                </div>
                <div class='kh_boxtwo'>
                <span class="iconfont">&#xe641;&#xe641;&#xe641;&#xe641;&#xe641;
                 <span class='kh_xinxin'> ${data[i].rating}</span>
                 <span class='kh_ordernum'>月售${data[i].recent_order_num}</span></span>
               <span><span class='kh_fnzs' >${data[i].delivery_mode.text}</span>
                <span class='kh_zsd'>准时达</span></span>
                </div>
                <div class='kh_boxbottom'>
                <span class='kh_psf'>
                 ￥${data[i].float_minimum_order_amount}起送/
                 ${data[i].piecewise_agent_fee.tips}
                 </span>
                 <span>${data[i].distance}
                 <span class='kh_order_lead_time'>/${data[i].order_lead_time}</span>
                </span></span>
                </div>
                </div>
                `)
                //点击跳转
                kh_Box[i].addEventListener('click', () => {
                    window.location.href = `/pages/restaurant/restaurant.html?id=${data[i].id}&point=${data[i].rating}`


                })

            }
        },
    })
    //选择地址
    let kh_dingweitext = document.getElementById("kh_dingweitext")
    kh_dingweitext.addEventListener('click', () => {
        window.location.href = '/pages/city/city.html'
    })

    let kh_tomyup = document.getElementById('kh_tomyup')
    kh_tomyup.addEventListener('click', () => {
        window.location.href = '/pages/Singin/singin.html'
    })




    //获取url地址
    let url = window.location.href;
    let paramStr = url.split('?')[1];
    let paramArr = [];
    let paramObj = {};
    if (paramStr) {
        paramArr = paramStr.split('&');
        for (let i = 0; i < paramArr.length; i++) {
            let p_item = paramArr[i];
            paramObj[p_item.split('=')[0]] = p_item.split('=')[1]
        }
    }
    // 获取缓存地址
    getLocation();
    function getLocation() {
        if (paramObj.geohash) {
            ajaxGetLocation();
        } else {
            if (window.localStorage.getItem('location')) {
                let location = window.localStorage.getItem('location');
                $('#kh_dingweitext').html(location);
            } else {
                ajaxGetLocation();
            }
        }

    }
    function ajaxGetLocation() {
        $().ajax({
            url: `https://elm.cangdu.org/v2/pois/${paramObj.geohash}`,
            method: 'GET',
            async: true,
            success: res => {
                $('#kh_dingweitext').html(res.name)
                window.localStorage.setItem('location', res.name);
            }
        })
    }
}

